import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login/login.vue'
import home from '../views/home/home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },

  {
    path: '/login',
    component: login
  },

  {
    path: '/home',
    component: home
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next)=>{
  if(to.path === '/login'){
    next()
    return
  }
  const tokenstr = window.sessionStorage.getItem('token')
  if(!tokenstr){
    next('/login')
    return
  }
  next()
})

export default router
